import $fetch from '@/api/fetch'
interface EditProposalData {
  proposalId: number
  proposalImg?: string
  projectName?: string
  projectDesc?: string
  proposalName?: string
  companyName?: string
  companyContacts?: string
  companyEmail?: string
  proposalFileUrl?: string
}
interface EditProposalModuleData {
  moudleId: string
  moudleImg?: string
  moudleFileUrl?: string
  moudleName?: string
}
export default () => {
  /**
   * 新增排板布局
   * 布局类型 1-子系统 2-设备 3-评分 4-节能价值 5-报价单
   */
  const uploadLayoutFile = async (
    type: 1 | 2 | 3 | 4 | 5,
    proposalLayoutType: number,
    file: File,
    itemType: string,
    layoutImg?: string,
  ): Promise<any> => {
    const response = await $fetch.uploadFile(
      '/system/layout/uploadLayout?layoutType=' +
        type +
        '&layoutImg=' +
        layoutImg +
        '&proposalLayoutType=' +
        proposalLayoutType + 
        '&itemType=' +
        itemType,
      file,
    )
    return response
  }
  /**
   * 编辑排板布局
   */
  const editLayoutFile = async (params: {
  layoutId: any;
  layoutName: string;
  proposalLayoutType: number;
  file: File;
  layoutImg?: string;
  itemType: string;
}): Promise<any> => {
    const { 
      layoutId, 
      layoutName, 
      proposalLayoutType, 
      file, 
      layoutImg, 
      itemType 
    } = params
    const response = await $fetch.uploadFile(
      `/system/layout/overrideUpload?layoutId=${layoutId}&layoutImg=${layoutImg}&layoutName=${layoutName}&proposalLayoutType=${proposalLayoutType}&itemType=${itemType}`,
      file,
    )
    return response
  }
  /**
   * 设为默认排版布局
   */
  const setDefaultLayout = async ({
    layoutId,
    layoutType,
    itemType,
    proposalLayoutType,
  }: {
    layoutId: string
    layoutType: 1 | 2 | 3 | 4 | 5
    itemType: string
    proposalLayoutType: 1 | 2
  }): Promise<any> => {
    const response = await $fetch.post('/system/layout/setDefault', {
      layoutId,
      itemType,
      layoutType,
      proposalLayoutType,
    })
    return response
  }
  /**
   * 设为默认模板
   */
  const setDefaultTemplate = async ({
    moudleId,
    itemType,
    moudleType,
  }: {
    moudleId: string
    itemType: string
    moudleType: 1 | 2
  }): Promise<any> => {
    const response = await $fetch.post('/system/proposalMoudle/setDefault', {
      moudleId,
      itemType,
      moudleType,
    })
    return response
  }

  /**
   * 上传提案数据文件
   */
  const uploadProposalDataFile = async (
    projectId: any,
    file: File,
  ): Promise<any> => {
    const response = await $fetch.uploadFile(
      `/system/proposalData/uploadPPTX?projectId=` + projectId,
      file,
    )
    return response
  }

  /**
   * 修改提案数据文件
   */
  const updateProposalDataFile = async (
    proposalId: number,
    file: File,
    downFile: File,
  ): Promise<any> => {
    const response = await $fetch.uploadFile(
      '/system/proposalData/overrideUpload?proposalId=' + proposalId,
      file, // list 文件
      downFile, // pptx 文件
    )
    return response
  }

  /**
   * 修改提案数据
   */
  const editProposalData = async (data: EditProposalData): Promise<any> => {
    const response = await $fetch.put('/system/proposalData', data)
    return response
  }

  /**
   * 新增提案数据
   */
  const addProposalData = async (data: {
    projectId: number
    proposalFileUrl: string
    [key: string]: any
  }): Promise<any> => {
    const response = await $fetch.post('/system/proposalData', data)
    return response
  }
  /**
   * 上传提案模板文件
   */
  const uploadProposalModuleFile = async (
    file: File,
    moudleType: 1 | 2,
    itemType: string,
    moudleImg?: string,
  ): Promise<any> => {
    const response = await $fetch.uploadFile(
      `/system/proposalMoudle/uploadMoudle?moudleImg=${moudleImg}&moudleType=${moudleType}&itemType=${itemType}`,
      file,
    )
    return response
  }

  /**
   * 修改提案模板文件
   */
  const updateProposalModuleFile = async (
    moudleId: any,
    itemType: string,
    file: File,
  ): Promise<any> => {
    const response = await $fetch.uploadFile(
      '/system/proposalMoudle/overrideUpload?moudleId=' + moudleId + '&itemType=' + itemType,
      file,
    )
    return response
  }

  /**
   * 修改提案模板信息
   */
  const editProposalModule = async (
    data: EditProposalModuleData,
  ): Promise<any> => {
    const response = await $fetch.put('/system/proposalMoudle', data)
    return response
  }

  /**
   * 复制提案数据,返回一个新的 projectId, 之后使用这个 id 进行添加
   */
  const copyProposalData = async (projectId: any): Promise<any> => {
    const response = await $fetch.get(
      '/system/proposalData/copyProposal/' + projectId,
    )
    return response
  }

  /**
   * 闭合提案数据
   */
  const closeProposalData = async (proposalId: any): Promise<any> => {
    const response = await $fetch.get(
      '/system/proposalData/closeProposal/' + proposalId,
    )
    return response
  }

  const urlToFileList = async (url: string): Promise<FileList> => {
    const response = await fetch(url)
    if (!response.ok) throw new Error('Network response was not ok')

    const arrayBuffer = await response.arrayBuffer()
    const blob = new Blob([arrayBuffer], {
      type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
    })

    // 从 URL 中提取文件名
    const urlParts = url.split('/')
    const fileName = urlParts[urlParts.length - 1]

    const file = new File([blob], fileName, { type: blob.type })
    const dataTransfer = new DataTransfer()
    dataTransfer.items.add(file)
    const fileList = dataTransfer.files
    return fileList
  }

  /**
   * 上传文件，返回文件地址
   */
  const uploadQiniu = async (file: File): Promise<any> => {
    const formData = new FormData()
    formData.append('file', file)
    const response = await $fetch.uploadFile('/system/common/uploadQiniu', file)
    return response
  }

  return {
    editLayoutFile,
    uploadLayoutFile,
    setDefaultLayout,
    setDefaultTemplate,
    uploadProposalDataFile,
    updateProposalDataFile,
    editProposalData,
    uploadProposalModuleFile,
    updateProposalModuleFile,
    editProposalModule,
    closeProposalData,
    addProposalData,
    copyProposalData,
    urlToFileList,
    uploadQiniu,
  }
}
